<template>
  <div class="page">
    <div>
      <van-cell-group>
        <van-field name="radio" label="用餐方式">
          <template #input>
            <van-radio-group v-model="dining_method" direction="horizontal">
              <van-radio name="1">堂食</van-radio>
              <van-radio name="2">外带</van-radio>
            </van-radio-group>
          </template>
        </van-field>
        <van-field v-model="remark" rows="2" autosize label="备注" type="textarea" maxlength="100" placeholder="请输入备注"
          show-word-limit />
      </van-cell-group>
    </div>

    <div class="contentBox bg-ff mt15">
      <div v-for="item in cartItems" :key="item.id" class="food-item flexBox aiCenter flexBetween">
        <div class="flexBox aiCenter">
          <div class="img-box">
            <img :src="handleImgSrc(item.photo_url)" alt="" class="food-img">
          </div>
          <div class="flexItem ml20">
            <div class="fs28 mb10">{{ item.name }}</div>
            <van-stepper v-model="item.quantity" integer />
          </div>
        </div>
        <div>
          <div class="fs24 c-666">x {{ item.quantity }}</div>
          <div class="fs26 f600 c-666">¥{{ item.fee }}</div>
          <div class="fs28 f600 c-f00">¥{{ (item.fee * item.quantity).toFixed(2) }}</div>
        </div>
      </div>
    </div>

    <div class="settlement bg-ff">
      <div class="flexBox flexBetween">
        <div class="fs28 ml30">共 <span class="c-main-1">{{ totalCount }}</span> 件商品，合计：<span class="f600 c-f00">
            ¥{{ totalPrice }}</span>
        </div>
        <div class="goPay fs28 textCenter" @click="handlePay">确认下单</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { handleImgSrc } from '@/utils'
import { computed } from 'vue'
import orderStore from '@/store/modules/order'
import { orderConfirmType } from '@/types/orderTypes'
import { showConfirmDialog, showToast } from 'vant'
import { placeOrder } from '@/api/menu'
import { menuStore } from '@/store'
import router from '@/router'

const order = orderStore()
const menu = menuStore()

const dining_method = ref(order.cartConfig.dining_method ? order.cartConfig.dining_method + '' : '1')
const remark = ref(order.cartConfig.remark || '')

const cartItems = computed<orderConfirmType[]>(() => {
  return order.cartItems
})

const totalCount = computed(() => {
  return cartItems.value.reduce((sum, item) => sum + item.quantity, 0)
})

const totalPrice = computed(() => {
  return cartItems.value.reduce((sum, item) => sum + item.fee * item.quantity, 0).toFixed(2)
})

const handlePay = async () => {
  showConfirmDialog({
    title: '提交订单',
    message: '提交后不可修改，如需调整请取消',
    confirmButtonColor: '#40a9ff'
  }).then(async () => {
    const params = {
      // 订单id存在时，为更新订单，否则为创建新订单
      order_id: order.cartConfig.order_id,
      dining_method: +dining_method.value,
      remark: remark.value,
      status: 1, // 1已支付 0待支付 
      items: cartItems.value.map(item => ({
        menu_id: item.id,
        quantity: item.quantity
      }))
    }
    if (!order.cartConfig.order_id) {
      delete params.order_id
    }

    const { data } = await placeOrder(params)
    if (data.code === 0) {
      showToast('下单成功')
      order.clearCart()
      menu.clearMenu()
      router.push('order')
    }
  }).catch(() => {
    return
  })
}
</script>

<style lang="scss" scoped>
.page {
  background: #F7F7F7;
  padding: 15px 15px 0 15px;
  box-sizing: border-box;

  .contentBox {
    flex: 1;
    border-radius: 10px;
    padding: 20px 30px 20px 20px;
    background: #fff;

    .food-item {
      padding: 15px 0;
      border-bottom: 1px solid #f5f5f5;
      position: relative;

      &:last-child {
        border-bottom: none;
      }

      .img-box {
        width: 120px;
        height: 120px;
        border-radius: 50%;
        overflow: hidden;
        background: #f5f5f5;
      }

      .food-img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
  }

  .settlement {
    border: 1px solid #EEEEEE;
    border-radius: 120px;
    height: 80px;
    line-height: 80px;
    box-sizing: border-box;
    position: absolute;
    bottom: 15px;
    left: 30px;
    right: 30px;

    .goPay {
      background: #40a9ff;
      color: #fff;
      border-radius: 120px;
      width: 150px;
      height: 80px;
      line-height: 80px;
    }
  }
}
</style>
